package com.yuntu.util;

import java.sql.*;

/**
 * @author MrWang-shuizhishuiheshui
 * @description 工具类  建立连接和释放资源
 */
public class JDBCUtils {
    private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    private static final String URL="jdbc:mysql://127.0.0.1:3306/myschool?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
    private static final String USER="root";
    private static final String PASSWORD="root";

    /**
     * 获取连接对象
     */
    public static Connection getConnect(){
        Connection con=null;
        try {
            //1.加载驱动
            Class.forName(DRIVER);
            //2.通过驱动管理员获取连接对象
            con= DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (ClassNotFoundException e) {
            System.out.println("架包加载有问题");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("连接加载有问题");
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 释放资源
     */
    public static void closeAll(Connection con, PreparedStatement ps , ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }



}
